/**
 * @file   GetClientListResponsePacket.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-05-05
 * @brief  DataBus Get Client List Response packet.
 *
 * Copyright (C) 2013  Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DataBus/Common/GetClientListResponsePacket.h>

bool DataBus::GetClientListResponsePacket::create(const quint8 destination,
                                                  const quint8 packetId,
                                                  const QList<quint8> &clientIdList,
                                                  Packet *packet)
{
    // Check parameters
    if ((destination == 0) ||
        (packet == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get the size of the list
    int size = clientIdList.size();

    if (size > 254)
    {
        // Error, number of items in the list is too big
        return false;
    }

    // Create Header
    packet->setSource(0u);
    packet->setDestination(destination);
    packet->setPacketType(PacketType_GetClientListResponse);
    packet->setPacketId(packetId);

    // Create Payload
    QByteArray payload;

    payload.append(static_cast<char>(size));

    for (int i = 0; i < size; i++)
    {
        payload.append(static_cast<char>(clientIdList.at(i)));
    }

    packet->setData(payload);

    // Success
    return true;
}

bool DataBus::GetClientListResponsePacket::parse(const Packet &packet,
                                                 QList<quint8> *clientIdList)
{
    // Check parameters
    if ((packet.getSource() != 0) ||
        (packet.getDestination() == 0) ||
        (packet.getPacketType() != PacketType_GetClientListResponse) ||
        (clientIdList == 0))
    {
        // Error, invalid parameters
        return false;
    }

    // Get data
    QByteArray data = packet.getData();

    if (data.size() < 1)
    {
        // Error, at least No of items should be in the response
        return false;
    }

    // Get no of items
    int noOfItems = data.at(0);

    // Check if payload size is valid
    if (data.size() != (noOfItems + 1))
    {
        // Error, invalid no of items
        return false;
    }

    // Get Client ID list
    clientIdList->clear();

    for (int i = 0; i < noOfItems; i++)
    {
        // Get Client ID
        quint8 clientId = static_cast<quint8>(data.at(1 + i));

        if (clientId == 0)
        {
            // Error, invalid Client ID (Client ID '0' is reserved for the server)
            return false;
        }
        else if (clientIdList->indexOf(clientId) >= 0)
        {
            // Error, this Client ID is already in the list
            return false;
        }

        // Add Client ID to the list
        clientIdList->append(clientId);
    }

    // Success
    return true;
}
